﻿using Demo.Net.MongoDB;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Microsoft.Extensions.DependencyInjection
{

    public static class MongoClientFactoryExtensions
    {

        /// <summary>
        /// 指定数据库名称，中间件形式获取
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="dbname"></param>
        /// <returns></returns>
        public static IServiceCollection AddMongoClient(this IServiceCollection services, string dbname, Action<MongoClientOption> options)
        {
            options = options ?? throw new ArgumentNullException(nameof(options));
            services = services ?? throw new ArgumentNullException(nameof(services));

            services.Configure(dbname, options);

            services.AddTransient<IMongoClientFactory, MongoClientFactory>();
            return services;
        }
    }
}